package cn.stylefeng.guns.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import groovyjarjarantlr.StringUtils;


public class AliyuncsUtil {
    public static class AliyunConfig{
        //测试环境
//        public static String regionId = "cn-hangzhou";
//        public static String accessKeyId = "LTAI4GKN8msGsVaHRW2XZFG3";
//        public static String secret = "MhbS5JlUp367S5w1uw2Wy8uWbVoVDy";
//        public static String SignName = "didilaji";
        //正式环境
        public static String regionId = "cn-hangzhou";
        public static String accessKeyId = "LTAI4GJ6zNgG6NDZCvZdursU";
        public static String secret = "xWJXJpm7eTxW8PWxvcd7amdjpfrWaG";
        public static String SignName = "黄蚂蚁集团";
    }

    private static String getTemplateCode(int type){
        if(type == 0){
            return "SMS_209140188";
        }else if(type == 1){
            //return "SMS_212480209";
            return "SMS_221058461";
        }else if(type == 2){
            //付款成功
            return "SMS_214517576";
        }else if(type == 3){
            //处理订单
            return "SMS_214527512";
        }else if(type == 4){
            //预付款通知
            return "SMS_214527484";
        }else if(type == 5){
            //取消订单
            return "SMS_214517564";

        }else if(type == 6){
            //退款申请通知
            return "SMS_225980628";
        }else if(type == 7){
            //新用户首次注册
            return "SMS_225990584";
        }else {
            return "";
        }
    }

    public static boolean sendSms(int type,String phone,String name,String ph){
        DefaultProfile profile = DefaultProfile.getProfile(AliyunConfig.regionId, AliyunConfig.accessKeyId, AliyunConfig.secret);
        IAcsClient client = new DefaultAcsClient(profile);

        String str="{  \"name\":\"xx\", \"phone\":\"yy\"}";
        String result="";
        if(name!=""&&ph!=""){
            result=str.replace("xx",name).replace("yy",ph);
        }

        System.out.println("result:"+result);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", AliyunConfig.regionId);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", AliyunConfig.SignName);
        request.putQueryParameter("TemplateCode", getTemplateCode(type));
        if (type==1||type==6||type==7){
            request.putBodyParameter("TemplateParam",result);

        }
        //"{\"name\":\"Tom\", \"code\":\"123\"}"

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println("发送短信返回:"+response.getData());
            AliyuncsMessage aliyuncsMessage = JSON.parseObject(response.getData(),AliyuncsMessage.class);
            if(aliyuncsMessage != null && aliyuncsMessage.getCode().equals("OK")){
                return true;
            }else {
                System.out.println(response.getData());
                return false;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean sendSms(int type,String phone,String code){
        DefaultProfile profile = DefaultProfile.getProfile(AliyunConfig.regionId, AliyunConfig.accessKeyId, AliyunConfig.secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", AliyunConfig.regionId);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", AliyunConfig.SignName);
        request.putQueryParameter("TemplateCode", getTemplateCode(type));
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println("发送短信返回:"+response.getData());
            AliyuncsMessage aliyuncsMessage = JSON.parseObject(response.getData(),AliyuncsMessage.class);
            if(aliyuncsMessage != null && aliyuncsMessage.getCode().equals("OK")){
                return true;
            }else {
                System.out.println(response.getData());
                return false;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean sendSmsOrder(int type,String phone,String orderType){
        DefaultProfile profile = DefaultProfile.getProfile(AliyunConfig.regionId, AliyunConfig.accessKeyId, AliyunConfig.secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", AliyunConfig.regionId);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", AliyunConfig.SignName);
        request.putQueryParameter("TemplateCode", getTemplateCode(type));
        request.putQueryParameter("TemplateParam", "{\"type\":\""+orderType+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println("发送短信返回:"+response.getData());
            AliyuncsMessage aliyuncsMessage = JSON.parseObject(response.getData(),AliyuncsMessage.class);
            if(aliyuncsMessage != null && aliyuncsMessage.getCode().equals("OK")){
                return true;
            }else {
                System.out.println(response.getData());
                return false;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
